package com.int_tech.utils;

import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.messionLog.dao.MessionLogDao;
import com.int_tech.messionLog.entity.MessionLog;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import sun.net.www.http.HttpClient;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.List;

class ThreadServerSocket implements Runnable {

    private Socket server;

    private int BUFFER_SIZE = 1024;
      @Autowired
      private MessionLogDao messionLogDao;
      @Autowired
      private MessionDao messionDao;
    public ThreadServerSocket(Socket i) {
        // TODO Auto-generated constructor stub
        server = i;
    }
    //接收处理牛总发来的数据
    public void saveClient(String data)
    {/*
        if(data.contains("open"))
        {
            String json = MulServerSocket.httpClientPost("http://47.94.45.64:8080/car_networking/cont/checkMession",data);
//        String json = MulServerSocket.httpClientPost("http://127.0.0.1:8080/carNetworking/cont/checkMession",data);
            System.out.print(json);
        }
        else
        {
            String json = MulServerSocket.httpClientPost("http://47.94.45.64:8080/car_networking/cont/checkRate",data);
//        String json = MulServerSocket.httpClientPost("http://127.0.0.1:8080/carNetworking/cont/checkRate",data);
            System.out.print(json);
        }*/
//        String json = MulServerSocket.httpClientPost("http://127.0.0.1/car_networking/cont/saveSocketLog",data);
        String json = MulServerSocket.httpClientPost("http://60.205.182.120:8080/HYJ/getData/saveSocketLog",data);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            InputStream in = server.getInputStream();
            OutputStream out = server.getOutputStream();
            byte[] recData = null;

            while(true) {
                recData = new byte[BUFFER_SIZE];
                int r = in.read(recData);
                //int r = in.read(recData);
                if(r>-1) {
                    String helloHex = DatatypeConverter.printHexBinary(recData);
                    System.out.printf("1111Hello hex: 0x%s", helloHex);
                    String data = helloHex;

                    if(data.trim().equals("over")) {
                        server.close();
                        break;
                    }

//                    data = bin2hex(data);
                    System.out.println("2222222222读取到客户端发送的来数据："+helloHex);

                    this.saveClient(helloHex);

                    out.write("这是服务端发给客户端的数据：".getBytes());
                    out.write(recData);
                }else {
                    System.out.println("数据读取完毕！");
                    server.close();
                    break;
                    //ss.close();
                }
            }


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                server.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public String bin2hex(String bin) {
        char[] digital = "0123456789ABCDEF".toCharArray();
        StringBuffer sb = new StringBuffer("");
        byte[] bs = bin.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(digital[bit]);
            bit = bs[i] & 0x0f;
            sb.append(digital[bit]);
        }
        return sb.toString();
    }

}

public class MulServerSocket{

    private static final int PORT = 8888;


    //private static final int BUFFER_SIZE = 1024;

    public static void start() throws IOException {
        // TODO Auto-generated method stub
        int counter = 1;
        ServerSocket ss = new ServerSocket(PORT);
        System.out.println("启动了");
        while(true) {
            Socket s = ss.accept();
            System.out.println("第 "+(counter++)+" 个连接");
            Thread t = new Thread(new ThreadServerSocket(s));
            t.start();
        }
    }


    public static String httpClientPost(String urlstr, String data) {
        try {

            org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
            HttpPost method = new HttpPost(urlstr);
//            method.setHeader("Content-Type","application/x-www-form-urlencoded");
            method.setHeader("Content-Type","application/json;charset=UTF-8");

            ByteArrayEntity reqEntity = new ByteArrayEntity(data.getBytes("UTF-8"));
            reqEntity.setContentEncoding("UTF-8");
            method.setEntity(reqEntity);
            HttpResponse response = null;

            response = httpclient.execute(method);
            if (response != null) {
                // HttpEntity resEntity = response.getEntity();
                return EntityUtils.toString(response.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}


